<template>
  <div class="container">
    <div class="app-container">
      <div class="left">
        <el-input v-model="params.keyword" style="margin-bottom:10px" type="text" prefix-icon="el-icon-search" size="small" placeholder="输入员工姓名全员搜索" @input="search" />
        <!-- 树形组件 -->
        <el-tree ref="tree" node-key="id" :data="deptlist" :props="{label:'name'}" default-expand-all :expand-on-click-node="false" highlight-current @node-click="nodeClick" />
      </div>
      <div class="right">
        <el-row class="opeate-tools" type="flex" justify="end">
          <el-button v-power="'add'" size="mini" type="primary" @click="$router.push('/employee/detail')">添加员工</el-button>
          <el-button v-power="'import_excel'" size="mini" @click="showimport=true">excel导入</el-button>
          <el-button v-power="'export_excel'" size="mini" @click="exporttable">excel导出</el-button>
        </el-row>
        <Importexcle :show-excel-dialog.sync="showimport" @success="getTable()" />
        <!-- 表格组件 -->
        <el-table :data="tablelist">
          <el-table-column align="center" label="头像">
            <template #default="{row}">
              <el-avatar v-if="row.staffPhoto" :src="row.staffPhoto" :size="30" />
              <span v-else class="username">{{ row.username?.charAt(0) }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="username" label="姓名" />
          <el-table-column prop="mobile" label="手机号" sortable />
          <el-table-column prop="workNumber" label="工号" sortable />
          <el-table-column label="聘用形式">
            <template #default="{row}">
              <span>{{ row.formOfEmployment===1?'正式':row.formOfEmployment===2?'非正式':'未知' }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="departmentName" label="部门" />
          <el-table-column prop="timeOfEntry" label="入职时间" sortable />
          <el-table-column label="操作" width="280px">
            <template #default="{ row }">
              <el-button size="mini" type="text" @click="$router.push(`/employee/detail/${row.id}`)">查看</el-button>
              <el-button size="mini" type="text" @click="viewrole(row.id)">角色</el-button>
              <el-popconfirm title="确认删除该行数据吗？" @confirm="delit(row.id)">
                <el-button slot="reference" style="margin-left:10px" size="mini" type="text">删除</el-button>
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页 -->
        <el-row style="height: 60px" align="middle" type="flex" justify="end">
          <el-pagination layout="total,prev, pager, next" :total="total" :current-page="params.page" :page-size="params.pagesize" @current-change="changePage" />
        </el-row>
        <!-- 分页 -->
      </div>
    </div>
    <el-dialog title="分配角色" :visible.sync="showrole" width="30%">
      <el-checkbox-group v-model="roleIds">
        <el-checkbox v-for="item in roleslist" :key="item.id" :label="item.id">{{ item.name }}</el-checkbox>
      </el-checkbox-group>
      <span slot="footer" class="dialog-footer">
        <el-button @click="showrole = false">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { gettree } from '@/utils/gettree'
import { getdept } from '@/api/department'
import { getemployee, exportemployee, delEmployee, getroles, getEmployeeDetail, updaterole } from '@/api/employee'
import FileSaver from 'file-saver'
import Importexcle from '@/views/employee/components/import-excel.vue'
export default {
  name: 'Employee',
  components: {
    Importexcle
  },
  data() {
    return {
      currentid: '',
      roleIds: [],
      roleslist: [],
      showimport: false,
      showrole: false,
      deptlist: [],
      tablelist: [],
      params: {
        departmentId: 1,
        page: 1, // 当前页码
        pagesize: 10,
        keyword: ''
      },
      total: 0 // 记录员工的总数
    }
  },
  created() {
    this.getdepts()
  },
  methods: {
    async submit() {
      await updaterole({ id: this.currentid, roleIds: this.roleIds })
      this.$message.success('分配角色成功')
      this.showrole = false
    },
    async viewrole(id) {
      this.currentid = id
      await this.getrolelist()
      this.showrole = true
    },
    async getrolelist() {
      const res = await getroles()
      console.log(res)
      this.roleslist = res
      const { roleIds } = await getEmployeeDetail(this.currentid)
      this.roleIds = roleIds
    },
    search() {
      clearTimeout(this.timer)
      this.timer = setTimeout(() => {
        this.params.page = 1
        this.getTable()
      }, 300)
    },
    async getdepts() {
      // 递归方法 将列表转化成树形
      // let result = await getDepartment()
      this.deptlist = gettree(await getdept(), 0)
      this.params.departmentId = this.deptlist[0].id
      this.$nextTick(async() => {
        this.$refs.tree.setCurrentKey(this.params.departmentId)
        this.getTable()
        // this.$refs.tree.setChecked([this.departmentId])
      })
    },
    async getTable() {
      const res = await getemployee(this.params)
      this.tablelist = res.rows
      this.total = res.total
    },
    nodeClick(data) {
      console.log(data)
      this.params.page = 1
      this.params.departmentId = data.id
      this.getTable()
    },
    async delit(id) {
      console.log(1)
      await delEmployee(id)
      this.getTable()
      if (this.tablelist.length === 1 && this.params.page > 1) this.params.page--
      this.$message.success('删除成功')
    },
    // 切换页码
    changePage(newPage) {
      this.params.page = newPage // 赋值新页码
      this.getTable() // 查询数据
    },
    async exporttable() {
      const res = await exportemployee()
      console.log(res)
      FileSaver.saveAs(res, '黑马员工表')
    }
  }
}
</script>

<style lang="scss" scoped>
.app-container {
  background: #fff;
  display: flex;
  .left {
    width: 280px;
    padding: 20px;
    border-right: 1px solid #eaeef4;
  }
  .right {
    flex: 1;
    padding: 20px;
    .opeate-tools {
      margin: 10px;
    }
    .username {
      height: 30px;
      width: 30px;
      line-height: 30px;
      text-align: center;
      border-radius: 50%;
      color: #fff;
      background: #04c9be;
      font-size: 12px;
      display: inline-block;
    }
  }
}
</style>
